package com.dingding.nettyserver.common.trasferrule.response;

import java.util.Arrays;

import com.dingding.nettyserver.common.protocolbuf.ErrorTipsProtocol.ErrorTips;
import com.dingding.nettyserver.common.trasferrule.request.Request;

/**
 * @Description 传送给客户端数据包的封装
 * @author fengshuonan
 * @date 2016年11月17日 上午11:07:12
 */
public class Response {
	
	/**
	 * 业务类型
	 */
	private byte bizType;
	
	/**
	 * 模块号
	 */
	private short module;
	
	/**
	 * 命令号
	 */
	private short cmd;
	
	/**
	 * 结果码
	 */
	private int stateCode = ResultCode.SUCCESS.getCode();
	
	/**
	 * 数据
	 */
	private byte[] data;
	
	public Response() {
	}
	
	public Response(Request message) {
		this.bizType = message.getBizType();
		this.module = message.getModule();
		this.cmd = message.getCmd();
	}
	
	public Response(byte bizType,short module, short cmd, byte[] data){
		this.bizType = bizType;
		this.module = module;
		this.cmd = cmd;
		this.data = data;
	}

	public int getStateCode() {
		return stateCode;
	}

	public void setStateCode(int stateCode) {
		this.stateCode = stateCode;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public short getModule() {
		return module;
	}

	public void setModule(short module) {
		this.module = module;
	}

	public short getCmd() {
		return cmd;
	}

	public void setCmd(short cmd) {
		this.cmd = cmd;
	}

	public byte getBizType() {
		return bizType;
	}

	public void setBizType(byte bizType) {
		this.bizType = bizType;
	}
	
	public void setErrorData(ResultCode code){
		ErrorTips tips = ErrorTips.newBuilder().setErrorMessage(code.getMessage()).build();
		this.setStateCode(code.getCode());
		this.setData(tips.toByteArray());
	}

	@Override
	public String toString() {
		return "Response [bizType=" + bizType + ", module=" + module + ", cmd=" + cmd + ", stateCode=" + stateCode
				+ ", data=" + Arrays.toString(data) + "]";
	}
	
}
